package org.mtmi.ui.scenes.actions;

import java.util.List;

import org.mtmi.ui.M;
import org.mtmi.ui.actions.Action;
import org.mtmi.ui.controls.Control;
import org.mtmi.ui.controls.listeners.ISelectionListener;
import org.mtmi.ui.controls.listeners.PopupListener;
import org.mtmi.ui.controls.listeners.SelectionEvent;
import org.mtmi.ui.controls.popups.AbstractButtonPopup;
import org.mtmi.ui.controls.popups.TwoButtonsPopup;
import org.mtmi.ui.drawables.IDrawable;
import org.mtmi.ui.drawables.SymbolDrawable;
import org.mtmi.ui.scenes.COLOR;
import org.mtmi.ui.scenes.ISceneManagerListener;
import org.mtmi.ui.scenes.Scene;
import org.mtmi.ui.scenes.SceneManager;

/**
 * Remove the control currently selected in actual scene, ask before remove.
 */
public class RemoveControlAction extends Action
implements ISceneManagerListener, ISelectionListener
{
	private SymbolDrawable image;
	private Scene scene;

	public RemoveControlAction() {
		super( PUSH );
		//TODO: this must be removed also ... but miss dispose() in IAction.		
		SceneManager.getDefault().addSceneManagerListener( this );
		scene = SceneManager.getDefault().getScene();
		scene.addSelectionListener( this );
	}
	
	@Override
	public String getText() {
		return "Remove Control";
	}
	
	@Override
	public IDrawable getImage() {
		if( image==null ) {
			image = new SymbolDrawable( COLOR.red, SymbolDrawable.CROSS );
		}
		return image;
	}
	
	@Override
	public boolean isEnabled() {
		return scene.hasSelection();
	}
	
	@Override
	public void run() {
		List<Control> selection = scene.getSelection();
		int count = selection.size();
		TwoButtonsPopup p = new TwoButtonsPopup( scene, "Delete selected "+count+" controls ?", M.SCENE_EDITOR_CONTROL );
		p.addResponseListener( new PopupListener() {
			@Override
			public void popupButtonSelected( int id ) {
				if( id == AbstractButtonPopup.OK ) {
					List<Control> selection = scene.getSelection();
					for( Control c : selection ) {
						c.dispose();
					}
					scene.setSelection( null );
				}
			}
		});
	}
	
	@Override
	public void sceneManagerModeChanged(boolean edit) {}

	@Override
	public void sceneManagerSceneChanged( Scene new_scene, Scene old_scene )
	{
		if( old_scene!=null ) old_scene.removeSelectionListener( this );
		scene = new_scene;
		scene.addSelectionListener( this );
	}

	@Override
	public void sceneManagerSceneAdded(Scene sceneAdded) {}
	
	@Override
	public void sceneManagerSceneRemoved(Scene sceneRemoved) {}

	@Override
	public void selectionChanged( SelectionEvent e ) {
		update();
	}
	
	@Override
	public void selectionConfirmed(SelectionEvent e) {}

}
